#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initMainwindow();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initMainwindow()
{
    QString imgPath = "/home/yyx/OpencvEnhance/girldiver.jpg"; // 本地路径(图片直接存放在项目目录下)
    Mat imgData = imread(imgPath.toLatin1().data()); //读取图片数据

    if (imgData.empty()) {
        qWarning("Failed to load image");
        return;
    }

    if (imgData.channels() == 3) {
        cvtColor(imgData, imgData, COLOR_BGR2RGB);//图片格式转换
    }

    myImg = imgData;
    myQImg = QImage((const unsigned char*)(imgData.data), imgData.cols, imgData.rows, imgData.step, QImage::Format_RGB888);
    imgShow();
}

void MainWindow::on_contrastVerticalSlider_sliderMoved(int position)
{
    imgProc(position / 33.3, 0);
}

void MainWindow::on_contrastVerticalSlider_valueChanged(int value)
{
    imgProc(value / 33.3, 0);
}

void MainWindow::on_brightnessVerticalSlider_sliderMoved(int position)
{
    imgProc(1.0, position);
}

void MainWindow::on_brightnessVerticalSlider_valueChanged(int value)
{
    imgProc(1.0, value);
}

void  MainWindow::imgShow()
{
    //Qt界面上显示图片
   ui->viewLabel->setPixmap(QPixmap::fromImage(myQImg.scaled(ui->viewLabel->size(), Qt::KeepAspectRatio)));
}

void MainWindow::imgProc(float contrast, int brightness)
{
    Mat imgSrc = myImg;
    Mat imgDst = Mat::zeros(imgSrc.size(), imgSrc.type());
    imgSrc.convertTo(imgDst, -1, contrast, brightness);

    // 将处理后的结果更新回 myQImg，并显示
    myQImg = QImage((const unsigned char*)(imgDst.data), imgDst.cols, imgDst.rows, imgDst.step, QImage::Format_RGB888);
    imgShow();
}

